<template>
  <div class="app-container">
    <el-form
      ref="form"
      class="form"
      :rules="rules"
      :model="form"
      label-width="120px"
    >
      <el-form-item label="优惠券名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入优惠券名称"></el-input>
      </el-form-item>

      <el-form-item label="优惠券类型" prop="type">
        <el-select
          style="width: 100%"
          v-model="form.type"
          placeholder="请选择优惠券类型"
        >
          <el-option
            :label="item.label"
            :value="item.value"
            :key="item.value"
            v-for="item in couponTypeList"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="金额" prop="amount">
        <el-input v-model="form.amount" placeholder="请输入金额"></el-input>
      </el-form-item>

      <template v-if="form.type === 1">
        <el-form-item label="满减限制" prop="fullMoney">
          <el-input
            v-model="form.fullMoney"
            placeholder="满多少可以使用，0无限制"
          ></el-input>
        </el-form-item>
      </template>

      <el-form-item v-if="form.type !== 2" label="发行数量" prop="publishCount">
        <el-input-number
          style="width: 100%"
          v-model="form.publishCount"
          :min="0"
          step-strictly
          placeholder="请输入发行数量，0表示无限制"
        ></el-input-number>
      </el-form-item>

      <template v-if="form.type !== 2">
        <el-form-item label="限制张数" prop="perLimit">
          <el-input
            v-model="form.perLimit"
            placeholder="每人限制使用张数，0无限制"
          ></el-input>
        </el-form-item>
      </template>

      <el-form-item label="备注">
        <el-input
          type="textarea"
          v-model="form.remark"
          placeholder="请输入备注"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{
          form.id ? "保存修改" : "立即创建"
        }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getCouponDetail, addOrEditCoupon } from "@/api/coupon/index";
import { couponTypeList } from "@/config/index";

export default {
  data() {
    return {
      form: {
        name: "",
        amount: "",
        fullMoney: "",
        type: "",
        perLimit: "",
        publishCount: "",
        remark: "",
      },
      couponTypeList,
      loading: false,
      rules: {
        name: [
          { required: true, message: "请输入优惠券名称", trigger: "blur" },
        ],
        type: [
          { required: true, message: "请选择优惠券类型", trigger: "change" },
        ],
        amount: [
          { required: true, message: "请输入金额", trigger: "blur" },
          {
            pattern: /^[0-9]*[1-9][0-9]*$/,
            message: "请输入正确的金额",
            trigger: "blur",
          },
        ],
        fullMoney: [
          { required: true, message: "请输入满减限制", trigger: "blur" },
          {
            pattern: /^[0-9]*[1-9][0-9]*$/,
            message: "请输入正确的金额",
            trigger: "blur",
          },
          {
            validator: (rule, value, callback) => {
              if (Number(value) < Number(this.form.amount)) {
                callback(new Error("满减限制金额不能小于优惠金额"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
        perLimit: [
          { required: true, message: "请输入限制张数", trigger: "blur" },
          {
            pattern: /^[0-9]*[1-9][0-9]*$/,
            message: "请输入正整数",
            trigger: "blur",
          },
        ],
        publishCount: [
          { required: true, message: "请输入发行数量", trigger: "blur" },
          {
            pattern: /^[0-9]*[1-9][0-9]*$/,
            message: "请输入正整数",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    if (this.$route.query.id) {
      this.getDetail();
    }
  },
  methods: {
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          addOrEditCoupon(this.form)
            .then((res) => {
              this.loading = false;
              this.$message.success("操作成功");
              this.$router.push("/coupon/list");
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    getDetail() {
      getCouponDetail(this.$route.query.id).then((res) => {
        this.form = res.data;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.form {
  width: 480px;
}
.disabled {
  ::v-deep {
    .el-upload--picture-card {
      display: none;
    }
  }
}
</style>
